package com.stylefeng.guns.rest.common;


import java.io.Serializable;


public class Result<T> implements Serializable{

        private String msg;

        private T data;

        private int code;

        private Boolean error;


        public String getMsg() {
            return msg;
        }

        public int getCode() {
            return code;
        }

        public T getData() {
            return data;
        }

        public Boolean getError() {
        return error;
    }


        public static <T> Result<T> fail(String message) {
            Result<T> result = new Result<>();
            result.msg = message;
            result.code(400);
            result.error(true);
            return result;

        }

        public static <T> Result<T> fail(String message, int code) {
            Result<T> result = new Result<>();
            result.msg = message;
            result.code(code);
            result.error(true);
            return result;

        }

        public static <T> Result<T> fail(String message, T data){
            Result<T> result = new Result<>();
            result.msg = message;
            result.error(true);
            result.data(data);
            return result;
        }
        private final static Result ok = new Result().code(200).error(false).msg("请求成功");


        public static <T> Result<T> ok() {
            return ok;
        }



        public static <T> Result<T> ok(T data) {
            Result<T> result = new Result<>();
            result.code(200);
            result.error(false);
            result.msg("请求成功");
            result.data(data);
            return result;

        }

        public static <T> Result<T> ok(T data, String msg) {
            Result<T> result = new Result<>();
            result.code(200);
            result.error(false);
            result.msg(msg);
            result.data(data);
            return result;
        }




        public Result<T> data(T data) {
            this.data = data;
            return this;
        }

        public Result<T> code(int code) {
            this.code = code;
            return this;
        }

        public Result<T> error(Boolean error) {
            this.error = error;
            return this;
        }

        public Result<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

    @Override
    public String toString() {
        return "{" +
                "msg='" + msg + '\'' +
                ", data=" + data +
                ", code=" + code +
                ", error=" + error +
                '}';
    }
}
